package org.ring.csr.filter;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.Filter;

public class NeedBracesOneLineFilter extends AutomaticBean implements Filter {

    public NeedBracesOneLineFilter() {
    }

    public boolean accept(AuditEvent aEvent) {
	boolean ret = true;
	if (aEvent.getSourceName().contains("NeedBracesCheck")) {
	    final String fileName = aEvent.getFileName();

	    try {
		FileInputStream fis = new FileInputStream(fileName);
		BufferedReader br = new BufferedReader(new InputStreamReader(
			fis));

		int lineNum = aEvent.getLine();

		for (int i = 0; i < lineNum - 1; ++i) {
		    br.readLine();
		}

		String line = br.readLine().trim();
		if (line.matches("^(if|.*else|for|while).*(;|return.*)$")) {
		    ret = false;
		}

		fis.close();
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
	return ret;
    }
}
